package com.kaifamiao.month03.day0330;

import java.util.Arrays;

public class Student {


    /**
     * 按照总分进行排序
     * @param scores 成绩数组
     * @param students 学生姓名数组
     */
    public static void sort(double[][] scores, String[] students) {
        sumScores(scores);
        int len = scores[0].length - 1; // 总成绩所在的下标
        for (int i = 0; i < scores.length - 1; i++) {
            for (int j = 0; j < scores.length - 1 - i; j ++) {
                if (scores[j][len] < scores[j + 1][len]) {
                    double[] temp = scores[j];
                    scores[j] = scores[j + 1];
                    scores[j + 1] = temp;  // 把成绩按照总分排序
                    String stu = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = stu; // 把学生名字也排序好
                }
            }
        }
    }

    /**
     * 先把总成绩放进成绩数组中
     * @param scores 成绩数组
     */
    public static void sumScores(double[][] scores) {
        int len = scores[0].length;
        for (int i = 0; i < scores.length; i++) {
            scores[i] = Arrays.copyOf(scores[i], len + 1);
            for (int j = 0; j < len; j++) {
                scores[i][len] += scores[i][j];
            }
        }
    }

    public static void main(String[] args) {
        String[] students = {"小王", "小张", "小吴", "小李"};
        String[] courses = {"C++", "Java", "Python"};
        double[][] scores = {
                {80.5, 100.2, 90.3},
                {98.5, 89.0, 99.0},
                {89.0, 99.5, 98.2},
                {99.6, 97.4, 90.9},
        };
        sort(scores,students);
        System.out.print("姓名" + "\t\t" + "总分" + "\t\t" + "排名" + "\t");
        for (String c : courses) {
            System.out.print(c + "\t\t");
        }
        System.out.println();
        for (int i = 0; i < scores.length; i ++) {
            System.out.print(students[i] + "\t\t");
            System.out.print(scores[i][scores[i].length - 1] + "\t");
            System.out.print(i + 1 + "\t");
            for (int j = 0; j < scores[i].length - 1; j ++) {
                System.out.print(scores[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
